package ch.sv7.tool.dbmanager.db.fixbuilder;

import ch.sv7.tool.dbmanager.Context;
import ch.sv7.tool.dbmanager.db.dialect.Dialect;
import ch.sv7.tool.dbmanager.db.difference.AbstractForeignKeyDifference;
import ch.sv7.tool.dbmanager.db.difference.SchemaDifference;
import ch.sv7.tool.dbmanager.db.fix.CreateForeignKeyFix;
import ch.sv7.tool.dbmanager.db.fix.DropForeignKeyFix;
import ch.sv7.tool.dbmanager.db.fix.SchemaFix;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class ForeignKeyParameterFixBuilder implements FilterFixBuilder {

	private String targetForeignKeyIdentifier;
	
	public SchemaFix[] createFixes(SchemaDifference difference, Dialect dialect, Context context) {
		AbstractForeignKeyDifference foreignKeyDifference = (AbstractForeignKeyDifference)difference;
		SchemaFix dropFix = new DropForeignKeyFix(foreignKeyDifference.getActualForeignKey());
		SchemaFix createFix = new CreateForeignKeyFix(foreignKeyDifference.getExpectedForeignKey());
		targetForeignKeyIdentifier = createFix.getActionTarget().getSchemaIdentifier();
		return new SchemaFix[]{ dropFix, createFix };
	}

	public boolean ignoreFix(SchemaFix fix) {
		String identifier = fix.getActionTarget().getSchemaIdentifier();
		/*
		 * Remove all editions on foreign keys that will be dropped
		 */
		if (identifier.equals(targetForeignKeyIdentifier) && fix.getAction() == SchemaFix.ACTION_EDIT){
			return true;
		} else {
			return false;
		}
	}

}
